package com.example.rlw.mvpplugintest.takephoto;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.ImageView;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.example.rlw.mvpplugintest.R;
import com.example.rlw.mvpplugintest.utils.AtomsUtils;
import com.example.rlw.mvpplugintest.utils.UriUtils;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.TResult;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 描述：拍照  选择图片
 * Created by rlw on 2019/3/20.
 */
public class TestTakePhotoActivity extends MyTakePhotoActivity implements OnItemClickListener {

    @BindView(R.id.select)
    Button mSelect;
    @BindView(R.id.image)
    ImageView mImage;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_take_photo_layout;
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {

    }

    @Override
    protected void initNet() {

    }

    @OnClick(R.id.select)
    public void onViewClicked() {
        new AlertView.Builder()
                .setContext(this)
                .setStyle(AlertView.Style.ActionSheet)
                .setTitle("上传头像")
                .setMessage(null)
                .setCancelText("取消")
                .setDestructive("拍照", "从相册中选择")
                .setOthers(null)
                .setOnItemClickListener(this)
                .build()
                .show();
    }

    @Override
    public void onItemClick(Object o, int position) {
        // 裁剪的配置参数
        CropOptions cropOptions = new CropOptions.Builder().setAspectX(1).setAspectY(1).setWithOwnCrop(true).create();
        Uri imageUri = UriUtils.getImageUri();
        // 点击弹窗中的条目
        switch (position) {
            case 0:
                AtomsUtils.makeText(this, "拍照");
                getTakePhoto().onPickFromCaptureWithCrop(imageUri, cropOptions);
                break;
            case 1:
                AtomsUtils.makeText(this, "从相册中选择");
                getTakePhoto().onPickFromDocumentsWithCrop(imageUri, cropOptions);
                break;
        }
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        Logger.e("路径" + result.getImage().getOriginalPath());
        Bitmap bitmap = BitmapFactory.decodeFile(result.getImages().get(0).getOriginalPath());
        mImage.setImageBitmap(bitmap);
    }
}
